<template>
  <el-tabs v-model="activeName">
    <el-tab-pane label="基础信息" name="first">
      <div> 
        <!-- 客户信息 --> 
         <FormPanel ref="formPanel" :list="formList" :cols="3" :formType="type" labelWidth="90" :isSubmitBtn="false" @formChange="formChangeHandle" />
        <!-- 意向信息 --> 
         <FormPanel ref="formPanel1" :list="formList1" :cols="3" :formType="type" labelWidth="100" :isSubmitBtn="false" @formChange="formChangeHandle" />
        <!-- 经销商信息 --> 
         <FormPanel ref="formPanel2" :list="formList2" :cols="3" :formType="type" labelWidth="100" :isSubmitBtn="false" @formChange="formChangeHandle" />
      </div> 
       <div
        :style="{ 
          position: 'fixed', 
          left: 0, 
          bottom: 0, 
          right: 0, 
          zIndex: 9, 
          borderTop: '1px solid #e9e9e9', 
          padding: '10px 20px', 
          background: '#fff', 
          textAlign: 'center' 
        }" 
      > 
        <el-button @click="closeDialog">取 消</el-button>
        <!-- <el-button @click="saveDialog" :disabled="isDisabled" type="primary">保 存</el-button> -->
        <AjaxButton :click="saveDialog" v-show="isShow" type="primary">保 存</AjaxButton>
      </div>
    </el-tab-pane>
    <el-tab-pane v-if="beijing" label="线索评级信息">
      <clueLevelModel :rowData="rowData" />
    </el-tab-pane>
    <!--

    <el-tab-pane label="线索评级" name="clueLevel">
      <div>
        <FormPanel
          ref="formPanel3"
          :list="formList3"
          :cols="1"
          :formType="type"
          labelWidth="180"
          :isSubmitBtn="false"
          @formChange="formChangeHandle"
        />
      </div>
    </el-tab-pane>
    <el-tab-pane label="用户画像" name="userPortrait">
      <div>
        <FormPanel
          ref="formPanel4"
          :list="formList4"
          :cols="3"
          :formType="type"
          labelWidth="150"
          :isSubmitBtn="false"
          @formChange="formChangeHandle"
        />
      </div>
    </el-tab-pane> -->
  </el-tabs>
</template>

<script>
import { queryClueSourceList, getOrgType, getDealerArea, getCompanyOrgIsMerge } from '@/api/potenCus/customer/potenCusManage';
import { addNewClues, getClueById, getTemperatureList, getMapCmsInfo } from '@/api/potenCus/customer/potenCusManageOEM';
import { getBrandList, getSeriesList, getModelList, getPackageList, getOptionList, getColorsList } from '@/api/potenCus/basedata/searchTestDriveCars';
import { getlistAllData } from '@/api/common/org/dealerCompany'; //城市接口
import AjaxButton from '@/components/AjaxButton';
import { dictionary } from '@/utils/dictMixin';
import { authority } from '@/utils/authMixin';
import searchDealers from './addOEMdiaolg/searchDealers';
import { phone, formatDates, formatDat, checkPassport } from '@/utils/validate';
import { getAppId } from '@/assets/js/auth';
import clueLevelModel from './clueLevelModel';

export default {
  name: 'OEMmodel',
  mixins: [dictionary, authority],
  components: {
    searchDealers,
    clueLevelModel
  },
  props: {
    type: {
      type: String,
      default: 'add'
    },
    rowData: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    this.saveData = null;
    return {
      formList: this.createFormList(),
      formList1: this.createFormList1(),
      formList2: this.createFormList2(),
      beijing:getAppId()=="beijing"?true:false,
      // formList3: this.createFormList3(),
      // formList4: this.createFormList4(),
      clueLevel: null,
      handleType: '',
      activeName: 'first',
      conf: { add: '新增', edit: '编辑' },
      isShow: true,
      allOrgId: []
    };
  },
  created() {
    this.init();
    this.getClueTree();
    this.getHotist();
    this.getCityList();
    this.getDictBrandList();
  },
  methods: {
    async init() {
      console.log('this.rowData', this.rowData, this.formList2);
      // 当前经销商有无小区 是否合并
      if (this.type === 'add' || this.type === 'edit') {
        const res = await getCompanyOrgIsMerge();
        console.log('是否合并', res);
        // isDisArea： 10041001 只有大区   10041002 ：有小区
        // isMerge ：10041001 销售售后合并  10041002 ：不合并
        let { isDisArea, isMerge } = res.data;
        // 1. 不合并只大区 传15061005
        if (isDisArea == 10041001 && isMerge == 10041002) {
          // 查询经销商代码
          const res1 = await getOrgType(15061005);
          // console.log('经销商代码1', res1.data)
          if (res1.resultCode == 200) {
            this.allOrgId = res1.data.map(x => ({ value: JSON.stringify(x), text: x.orgName }));
            console.log('经销商代码1', this.allOrgId);
            this.formList2.find(item => item.fieldName === 'orgId').itemList = JSON.parse(JSON.stringify(this.allOrgId));
            // this.formList2.find(item => item.fieldName ==='orgId').itemList = res1.data.map(x=> ({ value: x.orgId, text: x.orgName }))
          }
        }
        // 2. 不合并大小区 传15061005 ,15061007
        if (isDisArea == 10041002 && isMerge == 10041002) {
          const res2 = await getOrgType(15061005, 15061007);
          this.allOrgId = res2.data.map(x => ({ value: JSON.stringify(x), text: x.orgName }));
          console.log('经销商代码2', this.allOrgId);
          if (res2.resultCode == 200) {
            this.formList2.find(item => item.fieldName === 'orgId').itemList = JSON.parse(JSON.stringify(this.allOrgId));
          }
        }
        // 3. 合并大区 传15061004
        if (isDisArea == 10041001 && isMerge == 10041001) {
          const res3 = await getOrgType(15061014);
          this.allOrgId = res3.data.map(x => ({ value: JSON.stringify(x), text: x.orgName }));
          console.log('经销商代码3', this.allOrgId);
          if (res3.resultCode == 200) {
            this.formList2.find(item => item.fieldName === 'orgId').itemList = JSON.parse(JSON.stringify(this.allOrgId));
          }
        }
        // 4. 合并大小区 传15061014， 15061015
        if (isDisArea == 10041002 && isMerge == 10041001) {
          const res4 = await getOrgType(15061014, 15061015);
          this.allOrgId = res4.data.map(x => ({ value: JSON.stringify(x), text: x.orgName }));
          console.log('经销商代码4', this.allOrgId);
          if (res4.resultCode == 200) {
            this.formList2.find(item => item.fieldName === 'orgId').itemList = this.allOrgId;
          }
        }
      }
      // 这里和上面的顺序不能改啊
      if (this.type === 'edit') {
        this.clueId = this.rowData.clueId;
        this.isShow = true;
        await this.getEditInfoById();
      }
      if (this.type === 'show') {
        this.clueId = this.rowData.clueId;
        this.isShow = false;
        this.formList.splice(6, 1, {
          type: 'INPUT',
          label: '来源渠道',
          fieldName: 'valueType',
          placeholder: '',
          disabled: true,
          itemList: [],
          initialValue: ''
        });
        await this.getShowInfoById();
      }
      this.getCityList();

      //与map接口相关数据展示

      // const resMap = await getMapCmsInfo({ mobile: this.rowData.phone, appId: getAppId() });
      // if (resMap.resultCode == 200) {
      //   this.formList3.forEach(x => {
      //     if (x.fieldName == 'clueLevel') {
      //       this.clueLevel = resMap.data[x.fieldName];
      //     }
      //     if (resMap.data[x.fieldName]) {
      //       x.initialValue = resMap.data[x.fieldName];
      //     }
      //   });
      //   this.formList4.forEach(x => {
      //     if (resMap.data[x.fieldName]) {
      //       x.initialValue = resMap.data[x.fieldName];
      //     }
      //   });
      // }
    },
    preDictList(codes = []) {
      let { createDictList } = this;
      return this.flatten(codes.map(item => createDictList(item))).filter(x => x.value != 70001006 && x.value != 70001007 && x.value != 70001008);
    },
    flatten(list) {
      while (list.some(Array.isArray)) {
        list = [].concat(...list);
      }
      return list;
    },
    createFormList() {
      return [
        {
          type: 'BREAK_SPACE',
          label: '客户信息',
          style: {}
        },
        {
          type: 'INPUT',
          label: '客户名称',
          fieldName: 'name',
          maxlength: 30,
          placeholder: '请输入',
          rules: [{ required: true, message: '请输入客户名称', trigger: 'blur' }]
        },
        {
          type: 'SELECT',
          label: '客户类型',
          fieldName: 'custmorType',
          placeholder: '请选择',
          initialValue: '',
          itemList: this.createDictList('1523'),
          change: val => {
            console.log('val', val);
            if (val === 15231001) {
              this.formList.find(item => item.fieldName === 'custmorType').initialValue = 15231001;
              this.formList.find(item => item.fieldName === 'contactName').hidden = true;
              this.formList.find(item => item.fieldName === 'gender').label = '性别';
              this.formList.find(item => item.fieldName === 'phone').label = '手机号码';
            }
            if (val === 15231003) {
              // this.contactName == this.formList1.find(item => item.fieldName ==='customerName').initialValue
              this.formList.find(item => item.fieldName === 'custmorType').initialValue = 15231003;
              this.formList.find(item => item.fieldName === 'contactName').hidden = false;
              this.formList.find(item => item.fieldName === 'gender').label = '联系人性别';
              this.formList.find(item => item.fieldName === 'phone').label = '联系人手机';
            }
          },
          rules: [{ required: true, message: '请选择客户类型', trigger: 'blur' }]
        },
        {
          type: 'INPUT',
          label: '联系人姓名',
          fieldName: 'contactName',
          placeholder: '请输入',
          hidden: true
        },
        {
          type: 'SELECT',
          label: '性别',
          fieldName: 'gender',
          placeholder: '请选择',
          hidden: false,
          initialValue: 10021001,
          itemList: this.createDictList('1002')
        },
        {
          type: 'INPUT',
          label: '手机号码',
          fieldName: 'phone',
          maxlength: 11,
          placeholder: '请输入',
          disabled: false,
          rules: [
            { required: true, message: '请输入手机号码', trigger: 'blur' },
            { trigger: 'blur', validator: phone }
          ]
        },
        {
          type: 'INPUT_TREE',
          label: '来源渠道',
          fieldName: 'valueType',
          placeholder: '请选择',
          itemList: [],
          defaultExpandAll: true,
          disabled: this.type === 'edit' ? true : false,
          initialValue: '',
          rules: [{ required: true, message: '请选择来源渠道', trigger: 'change' }]
        },
        {
          type: 'INPUT',
          label: '来源备注',
          fieldName: 'sourceRemark',
          placeholder: '请选择'
        },
        {
          type: 'DATE',
          label: '采集时间',
          fieldName: 'handleTime',
          dateType: 'datetime',
          // format: 'yyyy-MM-dd HH:mm:ss',
          width: 200,
          placeholder: '请选择'
        },
        {
          type: 'DATE',
          label: '预购日期',
          fieldName: 'buyTime',
          dateType: 'exactdate',
          placeholder: '请选择'
        },
        {
          type: 'SELECT',
          label: '线索类型',
          fieldName: 'clueType',
          placeholder: '请选择',
          itemList: this.createDictList('7019')
        },
        {
          type: 'SELECT',
          label: '订金类型',
          fieldName: 'depositType',
          placeholder: '请选择',
          itemList: this.createDictList('8050')
        },
        {
          type: 'INPUT',
          label: '订金金额',
          fieldName: 'depositAmount',
          placeholder: '请选择'
        },
        // {   // TODO 2021-6-2 15:03:45 线索状态 经孟哥同意，隐藏掉；
        //   type: 'SELECT',
        //   label: '线索状态',
        //   fieldName: 'clueStatus',
        //   placeholder: '请选择',
        //   itemList: this.preDictList(['7000', '7067'])
        //   // itemList: [{"value":70671005,"text" :"待处理"},{"value":70671004,"text" :"CC清洗无效"}]
        // },
        {
          type: 'SELECT',
          label: '线索热度',
          fieldName: 'clueLevel',
          placeholder: '请选择',
          itemList: []
        },
        {
          type: 'SELECT',
          label: '省',
          fieldName: 'provinceId',
          placeholder: '请选择',
          initialValue: '',
          itemList: [],
          change: val => {
            console.log('省', val);
            console.log('listAllData', this.listAllData);
            if (val == 3662) {
              this.formList.find(item => item.fieldName === 'cityId').itemList = [{ text: '台湾', value: 3662 }];
              this.formList.find(item => item.fieldName === 'countyId').itemList = [{ text: '台湾', value: 3662 }];
            } else if (val == 3663) {
              this.formList.find(item => item.fieldName === 'cityId').itemList = [{ text: '香港', value: 3663 }];
              this.formList.find(item => item.fieldName === 'countyId').itemList = [{ text: '香港', value: 3663 }];
            } else if (val == 3664) {
              this.formList.find(item => item.fieldName === 'cityId').itemList = [{ text: '澳门', value: 3664 }];
              this.formList.find(item => item.fieldName === 'countyId').itemList = [{ text: '澳门', value: 3664 }];
            }
            let city = [];
            this.formList.find(item => item.fieldName === 'cityId').initialValue = '';
            this.formList.find(item => item.fieldName === 'countyId').initialValue = '';

            this.formList.find(item => item.fieldName === 'cityId').itemList = [];
            this.formList.find(item => item.fieldName === 'countyId').itemList = [];
            this.listAllData.forEach(x => {
              // 筛选省
              if (x.parentRegionId == val) {
                city.push({ text: x.regionName, value: x.regionId });
                this.formList.find(item => item.fieldName === 'cityId').itemList = _.uniqBy(city, 'value');
              }
            });
          }
        },
        {
          type: 'SELECT',
          label: '市',
          fieldName: 'cityId',
          placeholder: '请选择',
          initialValue: '',
          itemList: [],
          change: val => {
            console.log('市', val);
            let area = [];
            this.formList.find(item => item.fieldName === 'countyId').initialValue = '';
            this.formList.find(item => item.fieldName === 'countyId').itemList = [];
            this.listAllData.forEach(x => {
              // 筛选省
              if (x.parentRegionId == val) {
                // console.log('val111',val)
                area.push({ text: x.regionName, value: x.regionId });
                this.formList.find(item => item.fieldName === 'countyId').itemList = _.uniqBy(area, 'value');
              }
            });
          }
        },
        {
          type: 'SELECT',
          label: '区',
          fieldName: 'countyId',
          placeholder: '请选择',
          initialValue: '',
          itemList: []
        },
        {
          type: 'INPUT',
          label: '地址',
          fieldName: 'address',
          placeholder: '请输入'
        },
        {
          type: 'TEXT_AREA',
          label: '备注',
          fieldName: 'remark',
          selfCols: 2,
          rows: 1,
          placeholder: '请输入'
        },
        {
          type: 'INPUT',
          label: '清洗次数',
          fieldName: 'cleaTime',
          disabled: true
        }
      ];
    },
    // 意向信息
    createFormList1() {
      return [
        {
          type: 'BREAK_SPACE',
          label: '意向信息',
          style: {}
        },
        {
          type: 'SELECT',
          label: '品牌',
          fieldName: 'modeBrandCode',
          placeholder: '请选择',
          itemList: [],
          change: val => {
            this.formList1.find(item => item.fieldName === 'modeSeriesCode').initialValue = '';
            this.formList1.find(item => item.fieldName === 'modelCode').initialValue = '';
            this.formList1.find(item => item.fieldName === 'modeConfigCode').initialValue = '';
            this.formList1.find(item => item.fieldName === 'modeOptionCode').initialValue = '';
            this.formList1.find(item => item.fieldName === 'modeColorCode').initialValue = '';
            this.changeBrandSelect(val);
          }
        },
        {
          type: 'SELECT',
          label: '车系',
          fieldName: 'modeSeriesCode',
          placeholder: '请选择',
          itemList: [],
          change: val => {
            this.formList1.find(item => item.fieldName === 'modelCode').initialValue = '';
            this.formList1.find(item => item.fieldName === 'modeConfigCode').initialValue = '';
            this.formList1.find(item => item.fieldName === 'modeOptionCode').initialValue = '';
            this.formList1.find(item => item.fieldName === 'modeColorCode').initialValue = '';
            this.changeSeriesSelect(val);
          }
        },
        {
          type: 'SELECT',
          label: '车型',
          fieldName: 'modelCode',
          placeholder: '请选择',
          itemList: [],
          change: val => {
            this.formList1.find(item => item.fieldName === 'modeConfigCode').initialValue = '';
            this.formList1.find(item => item.fieldName === 'modeOptionCode').initialValue = '';
            this.formList1.find(item => item.fieldName === 'modeColorCode').initialValue = '';
            this.changeModelSelect(val);
          }
        },
        {
          type: 'SELECT',
          label: '配置',
          fieldName: 'modeConfigCode',
          placeholder: '请选择',
          itemList: [],
          change: val => {
            this.formList1.find(item => item.fieldName === 'modeOptionCode').initialValue = '';
            this.formList1.find(item => item.fieldName === 'modeColorCode').initialValue = '';
            this.changeOptionSelect(val);
          }
        },
        {
          type: 'SELECT',
          label: '选装',
          fieldName: 'modeOptionCode',
          placeholder: '请选择',
          itemList: [],
          change: val => {
            this.formList1.find(item => item.fieldName === 'modeColorCode').initialValue = '';
            this.changeColorSelect(val);
          }
        },
        {
          type: 'SELECT',
          label: '颜色',
          fieldName: 'modeColorCode',
          placeholder: '请选择',
          itemList: []
        }
      ];
    },
    // 经销商信息
    createFormList2() {
      return [
        {
          type: 'BREAK_SPACE',
          label: '经销商信息',
          style: {}
        },
        {
          type: 'SELECT',
          label: '所属区域',
          fieldName: 'orgId',
          placeholder: '请选择',
          itemList: [],
          change: orgID => {
            console.log('orgID', orgID);
            let newOrgId = orgID ? JSON.parse(orgID).orgId : '';
            if (newOrgId) {
              getDealerArea(newOrgId, '', '').then(res => {
                console.log('根据事业部查询经销商', res);
                if (res.resultCode === 200) {
                  this.formList2.find(item => item.fieldName === 'ownerName').itemList = res.data.map(x => ({ value: x.dealerId, text: x.dealerShortname, $code: x.dealerCode }));
                }
              });
            } else {
              this.formList2.find(item => item.fieldName === 'ownerName').itemList = [];
              this.formList2.find(item => item.fieldName === 'orgId').itemList = JSON.parse(JSON.stringify(this.allOrgId));
            }
            this.formList2.find(item => item.fieldName === 'ownerName').initialValue = '';
            this.formList2.find(item => item.fieldName === 'dealerCode').initialValue = '';
          }
        },
        {
          type: 'SELECT',
          label: '经销商简称',
          selfCols: 2,
          fieldName: 'ownerName',
          placeholder: '请选择',
          itemList: [],
          change: e => {
            if (typeof e == 'number') {
              let itemList = this.formList2.find(item => item.fieldName === 'ownerName').itemList;
              let result = itemList.find(item => item.value == e)['$code'];
              this.formList2.find(val => val.fieldName === 'dealerCode').initialValue = result;
            } else {
              this.formList2.find(item => item.fieldName === 'dealerCode').initialValue = '';
            }
          }
        },
        {
          type: 'INPUT',
          label: '经销商代码',
          fieldName: 'dealerCode',
          placeholder: '请输入',
          initialValue: '',
          unitRender: () => {
            return <el-button slot="append" icon="el-icon-search" onClick={this.openSearch}></el-button>;
          },
          change: val => {
            console.log('经销商代码', val, typeof val);
            if (typeof val == 'string' || !val) {
              this.formList2.find(item => item.fieldName === 'orgId').itemList = JSON.parse(JSON.stringify(this.allOrgId));
              this.formList2.find(item => item.fieldName === 'ownerName').itemList = [];
              this.formList2.find(item => item.fieldName === 'orgId').initialValue = '';
              this.formList2.find(item => item.fieldName === 'ownerName').initialValue = '';
            }
          }
        }
      ];
    },

    // 线索评级
    createFormList3() {
      return [
        {
          type: 'INPUT',
          label: '线索评级等级',
          fieldName: 'clueLevel',
          // hidden: true,
          render: props => {
            return <el-rate disabled={true} v-model={this.clueLevel}></el-rate>;
          },
          disabled: true
        },
        {
          type: 'INPUT',
          label: '线索评级得分',
          fieldName: 'clueScore',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '线索评级身份得分',
          fieldName: 'clueDataScore',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '线索评级行为得分',
          fieldName: 'clueTagScore',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '沟通热度',
          fieldName: 'communicationLevel',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '意向匹配度',
          fieldName: 'tempwst1',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '复购意向',
          fieldName: 'repurchaseLevel',
          disabled: true
        }
      ];
    },
    // 用户画像
    createFormList4() {
      return [
        {
          type: 'INPUT',
          label: '客户类型',
          fieldName: 'customerType',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '人群圈选标识名称',
          fieldName: 'cdpTag',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '标签信息',
          fieldName: 'TEMPWST2',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '姓名',
          fieldName: 'name',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '性别',
          fieldName: 'gender',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '昵称',
          fieldName: 'nickName',
          disabled: true
        },
        {
          type: 'INPUT',
          label: 'vin号',
          fieldName: 'vin',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '微信openid',
          fieldName: 'TEMPWST3',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '生日',
          fieldName: 'birthday',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '省份',
          fieldName: 'province',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '城市',
          fieldName: 'city',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '职业',
          fieldName: 'occupation',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '学历',
          fieldName: 'education',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '婚姻',
          fieldName: 'marriage',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '手机号码',
          fieldName: 'mobile',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '身份证',
          fieldName: 'TEMPWST5',
          disabled: true
        }
      ];
    },
    deepListClone(data) {
      let tempList = [];
      // console.log('deepListClone',Object.prototype.toString.call(data),data)
      if (data && data.length && Object.prototype.toString.call(data) == '[object Array]') {
        for (let i in data) {
          if (data[i].children && [].concat(data[i].children).length > 0) {
            tempList[i] = {
              text: data[i].clueName,
              value: data[i].value,
              parentId: data[i].parentId,
              disabled: true,
              children: this.deepListClone([].concat(data[i].children))
            };
          } else {
            tempList[i] = {
              text: data[i].clueName,
              value: data[i].value,
              parentId: data[i].parentId,
              disabled: false
            };
          }
        }
      }
      return tempList;
    },
    // 线索热度定义
    async getHotist() {
      const res = await getTemperatureList();
      // console.log('热度定义', res)
      if (res.resultCode === 200) {
        this.formList.find(item => item.fieldName === 'clueLevel').itemList = res.data.map(x => ({ value: x.id, text: x.clueHeatName }));
      }
    },
    // 潜客来源
    async getClueTree() {
      const res = await queryClueSourceList();
      // console.log('潜客来源', res)
      let treeList = this.deepListClone([].concat(res.data));
      this.formList.find(item => item.fieldName === 'valueType').itemList = treeList;
      // treeList.forEach(el => (el.text = el.clueName));
      // treeList.forEach(el => (el.value = el.id));
      // treeList.forEach(el => (el.parentId = el.parentId));
      // treeList.forEach(el => el.disabled = el.clueLevel == 1 ? true : false);
    },
    // 获取编辑明细
    async getEditInfoById() {
      const res = await getClueById(this.clueId);
      console.log('编辑数据', res);
      if (res.resultCode === 200) {
        this.formList.forEach(x => {
          if (x.fieldName == 'cleaTime' && res.data[x.fieldName] == null) x.initialValue = 0;
          else x.initialValue = res.data[x.fieldName];
        });
        this.formList.find(item => item.fieldName === 'gender').initialValue = Number(res.data.gender);
        this.formList1.forEach(x => {
          if (res.data[x.fieldName]) {
            x.initialValue = res.data[x.fieldName];
          }
        });

        console.log('编辑的客户类型', res.data.custmorType);
        if (res.data.custmorType === 15231001) {
          this.formList.find(item => item.fieldName === 'contactName').hidden = true;
          this.formList.find(item => item.fieldName === 'gender').label = '性别';
          this.formList.find(item => item.fieldName === 'phone').label = '手机号码';
        }
        if (res.data.custmorType === 15231003) {
          // this.contactName == this.formList1.find(item => item.fieldName ==='customerName').initialValue
          this.formList.find(item => item.fieldName === 'contactName').hidden = false;
          this.formList.find(item => item.fieldName === 'contactName').initialValue = res.data.contactName;
          this.formList.find(item => item.fieldName === 'gender').label = '联系人性别';
          this.formList.find(item => item.fieldName === 'phone').label = '联系人手机';
        }
        // if(res.data.dealerCode == null){
        //   this.formList2.find()
        // }
        // this.formList2.forEach(x =>{
        //   x.initialValue = res.data[x.fieldName]
        // })
        // console.log('编辑明细dealerCode',this.formList2)
        this.formList2.find(item => item.fieldName == 'dealerCode').initialValue = res.data['dealerCode'];
        let hasValue = this.formList2.find(item => item.fieldName == 'dealerCode').initialValue;
        if (hasValue != null || hasValue != '' || hasValue != undefined) {
          this.openSearch();
        }
        //编辑过来数据
        this.changeBrandSelect(res.data.modeBrandCode);
        this.changeSeriesSelect(res.data.modeSeriesCode);
        this.changeModelSelect(res.data.modelCode);
        this.changeOptionSelect(res.data.modeConfigCode);
        this.changeColorSelect(res.data.modeOptionCode);
      }
    },
    //getShowInfoById
    async getShowInfoById() {
      const res = await getClueById(this.clueId);
      console.log('明细数据', res);
      if (res.resultCode === 200) {
        this.formList.forEach(x => {
          x.initialValue = res.data[x.fieldName];
        });

        this.formList1.forEach(x => {
          x.initialValue = res.data[x.fieldName];
        });

        this.formList2.forEach(x => {
          x.initialValue = res.data[x.fieldName];
        });
        let hasValue = this.formList2.find(item => item.fieldName == 'dealerCode').initialValue;
        if (hasValue != null || hasValue != '' || hasValue != undefined) {
          this.openSearch();
        }
        this.formList.find(item => item.fieldName === 'gender').initialValue = Number(res.data.gender);
        this.formList.find(item => item.fieldName === 'provinceId').initialValue = res.data.province;
        this.formList.find(item => item.fieldName === 'cityId').initialValue = res.data.city;
        this.formList.find(item => item.fieldName === 'countyId').initialValue = res.data.county;
        this.formList.find(item => item.fieldName === 'valueType').initialValue = res.data.sourceStr;

        this.formList1.find(item => item.fieldName === 'modeBrandCode').initialValue = res.data.modeBrand;
        this.formList1.find(item => item.fieldName === 'modeSeriesCode').initialValue = res.data.modeSeries;
        this.formList1.find(item => item.fieldName === 'modelCode').initialValue = res.data.model;
        this.formList1.find(item => item.fieldName === 'modeConfigCode').initialValue = res.data.modeConfig;
        this.formList1.find(item => item.fieldName === 'modeOptionCode').initialValue = res.data.modeOption;
        this.formList1.find(item => item.fieldName === 'modeColorCode').initialValue = res.data.modeColor;
      }
    },
    // 意向车型
    async getDictBrandList() {
      const res = await getBrandList();
      // console.log("品牌YX",res)
      if (res.resultCode === 200) {
        this.formList1.find(item => item.fieldName === 'modeBrandCode').itemList = res.data.map(x => ({ value: `${x.brandId}`, text: x.brandName }));
      }
    },
    async changeBrandSelect(brandsid) {
      if (!brandsid) {
        this.formList1.find(item => item.fieldName === 'modeSeriesCode').initialValue = '';
        this.formList1.find(item => item.fieldName === 'modeSeriesCode').itemList = [];
        this.formList1.find(item => item.fieldName === 'modelCode').initialValue = '';
        this.formList1.find(item => item.fieldName === 'modelCode').itemList = [];
        this.formList1.find(item => item.fieldName === 'modeConfigCode').initialValue = '';
        this.formList1.find(item => item.fieldName === 'modeConfigCode').itemList = [];
        let modeOptionCode = this.formList.find(item => item.fieldName === 'modeOptionCode');
        modeOptionCode.initialValue = '';
        modeOptionCode.itemList = [];
        this.formList1.find(item => item.fieldName === 'modeColorCode').initialValue = '';
        this.formList1.find(item => item.fieldName === 'modeColorCode').itemList = [];
      } else {
        const res = await getSeriesList(brandsid);
        if (res.resultCode === 200) {
          // console.log("车系YX",res)
          this.formList1.find(item => item.fieldName === 'modeSeriesCode').itemList = res.data.map(x => ({ value: `${x.seriesId}`, text: x.seriesName }));
        }
        // if (this.formList1.find(item => item.fieldName ==='modeSeriesCode').initialValue ) {
        //   this.formList1.find(item => item.fieldName ==='modeSeriesCode').initialValue = '';
        //   this.formList1.find(item => item.fieldName ==='modelCode').initialValue = '';
        //   this.formList1.find(item => item.fieldName ==='modeConfigCode').initialValue = '';
        //   this.formList1.find(item => item.fieldName ==='modeColorCode').initialValue = '';
        // }
      }
    },
    async changeSeriesSelect(seriessid) {
      const brandsid = this.formList1.find(item => item.fieldName === 'modeBrandCode').initialValue;
      if (!brandsid || !seriessid) {
        this.formList1.find(item => item.fieldName === 'modelCode').initialValue = '';
        this.formList1.find(item => item.fieldName === 'modelCode').itemList = [];
        this.formList1.find(item => item.fieldName === 'modeConfigCode').initialValue = '';
        this.formList1.find(item => item.fieldName === 'modeConfigCode').itemList = [];
        let modeOptionCode = this.formList.find(item => item.fieldName === 'modeOptionCode');
        modeOptionCode.initialValue = '';
        modeOptionCode.itemList = [];
        this.formList1.find(item => item.fieldName === 'modeColorCode').initialValue = '';
        this.formList1.find(item => item.fieldName === 'modeColorCode').itemList = [];
      } else {
        const res = await getModelList(brandsid, seriessid);
        if (res.resultCode === 200) {
          // console.log("车型YX",res)
          this.formList1.find(item => item.fieldName === 'modelCode').itemList = res.data.map(x => ({ value: `${x.modelId}`, text: x.modelName }));
        }
        if (this.formList1.find(item => item.fieldName === 'modelCode').initialValue) {
          // this.formList1.find(item => item.fieldName ==='modelCode').initialValue = '';
          // this.formList1.find(item => item.fieldName ==='modeConfigCode').initialValue = '';
          // this.formList1.find(item => item.fieldName ==='modeColorCode').initialValue = '';
        }
      }
    },
    async changeModelSelect(modelId) {
      const brandsid = this.formList1.find(item => item.fieldName === 'modeBrandCode').initialValue;
      const seriessid = this.formList1.find(item => item.fieldName === 'modeSeriesCode').initialValue;
      if (!brandsid || !seriessid || !modelId) {
        this.formList1.find(item => item.fieldName === 'modeConfigCode').initialValue = '';
        this.formList1.find(item => item.fieldName === 'modeConfigCode').itemList = [];
        let modeOptionCode = this.formList.find(item => item.fieldName === 'modeOptionCode');
        modeOptionCode.initialValue = '';
        modeOptionCode.itemList = [];
        this.formList1.find(item => item.fieldName === 'modeColorCode').initialValue = '';
        this.formList1.find(item => item.fieldName === 'modeColorCode').itemList = [];
      } else {
        const res = await getPackageList(modelId);
        if (res.resultCode === 200) {
          // console.log("配置YX",res)
          this.formList1.find(item => item.fieldName === 'modeConfigCode').itemList = res.data.map(x => ({ value: `${x.packageId}`, text: x.configName }));
        }
        if (this.formList1.find(item => item.fieldName === 'modeConfigCode').initialValue) {
          // this.formList1.find(item => item.fieldName ==='modeConfigCode').initialValue = '';
          // this.formList1.find(item => item.fieldName ==='modeColorCode').initialValue = '';
        }
      }
    },
    //变更选装选择框
    async changeOptionSelect(packageId) {
      const brandsid = this.formList1.find(item => item.fieldName === 'modeBrandCode').initialValue;
      const seriessid = this.formList1.find(item => item.fieldName === 'modeSeriesCode').initialValue;
      const modelId = this.formList1.find(item => item.fieldName === 'modelCode').initialValue;
      if (!brandsid || !seriessid || !modelId || !packageId) {
        let modeOptionCode = this.formList1.find(item => item.fieldName === 'modeOptionCode');
        modeOptionCode.initialValue = '';
        modeOptionCode.itemList = [];
        this.formList1.find(item => item.fieldName === 'modeColorCode').initialValue = '';
        this.formList1.find(item => item.fieldName === 'modeColorCode').itemList = [];
      } else {
        const res = await getOptionList(packageId);
        if (res.resultCode === 200) {
          // console.log("颜色YX",res)
          console.log(
            '查询其表格 =>',
            this.formList1.find(item => item.fieldName === 'modeOptionCode')
          );
          this.formList1.find(item => item.fieldName === 'modeOptionCode').itemList = res.data.map(x => ({ value: `${x.optionId}`, text: x.optionName }));
        }
      }
    },
    //变更颜色选择框
    async changeColorSelect(optionId) {
      const brandsid = this.formList1.find(item => item.fieldName === 'modeBrandCode').initialValue;
      const seriessid = this.formList1.find(item => item.fieldName === 'modeSeriesCode').initialValue;
      const modelId = this.formList1.find(item => item.fieldName === 'modelCode').initialValue;
      const packageId = this.formList1.find(item => item.fieldName === 'modeConfigCode').initialValue;
      if (!brandsid || !seriessid || !modelId || !optionId) {
        this.formList1.find(item => item.fieldName === 'modeColorCode').initialValue = '';
        this.formList1.find(item => item.fieldName === 'modeColorCode').itemList = [];
      } else {
        const res = await getColorsList(optionId);
        if (res.resultCode === 200) {
          // console.log("颜色YX",res)
          this.formList1.find(item => item.fieldName === 'modeColorCode').itemList = res.data.map(x => ({ value: `${x.productId}`, text: x.colorName }));
        }
        if (this.formList1.find(item => item.fieldName === 'modeColorCode').initialValue) {
          // this.formList1.find(item => item.fieldName ==='modeColorCode').initialValue = '';
        }
      }
    },
    async getCityList() {
      await getlistAllData().then(res => {
        // console.log('城市', res)
        let province = [];
        let initCity = [];
        let initCounty = [];
        this.listAllData = res.data;
        this.listAllData.forEach(x => {
          // 筛选省
          if (x.parentRegionId == null) {
            province.push({ text: x.regionName, value: x.regionId });
          }
          // 市
          if (x.parentRegionId == this.formList.find(item => item.fieldName === 'provinceId').initialValue) {
            initCity.push({ text: x.regionName, value: x.regionId });
          }
          if (x.parentRegionId == this.formList.find(item => item.fieldName === 'cityId').initialValue) {
            initCounty.push({ text: x.regionName, value: x.regionId });
          }
        });
        // console.log(province,initCity,initCounty,"initCounty",this.rowData,this.formList.find(item => item.fieldName === 'provinceId').initialValue)
        this.formList.find(item => item.fieldName === 'provinceId').itemList = province;
        this.formList.find(item => item.fieldName === 'cityId').itemList = initCity;
        this.formList.find(item => item.fieldName === 'countyId').itemList = initCounty;
      });
    },
    async saveDialog() {
      console.log(this.saveData, 'this.saveData');
      this.$refs.formPanel.SUBMIT_FORM();
      const [err1, data1] = await this.$refs.formPanel1.GET_FORM_DATA();
      const [err2, data2] = await this.$refs.formPanel2.GET_FORM_DATA();
      // 特别处理
      if (err1) return;
      if (err2) return;
      let data222 = JSON.parse(JSON.stringify(data2));
      data222.orgId = data222.orgId ? JSON.parse(data222.orgId).orgId : '';
      if (this.saveData !== null) {
        let saveObj = {
          ...this.saveData,
          ...data1,
          ...data222
        };
        console.log('saveObj', saveObj);
        let res = {};
        if (this.type == 'add') {
          res = await addNewClues(saveObj);
        }
        if (this.type === 'edit') {
          saveObj['clueId'] = this.clueId;
          res = await addNewClues(saveObj);
        }
        if (res.resultCode === 200) {
          this.closeDialog('refresh');
          this.$message.success('保存成功');
        }
      } else {
        this.$message.warning('请填写必填项!');
      }
    },
    closeDialog(param) {
      this.$emit('close', false, param);
    },
    async openSearch() {
      console.log(typeof this.formList2.find(item => item.fieldName == 'dealerCode').initialValue, 'val');
      let dealerCodeVal = this.formList2.find(item => item.fieldName == 'dealerCode').initialValue;
      if (dealerCodeVal == '' || dealerCodeVal == null || dealerCodeVal == undefined) {
        console.log('请输入点内容不好么');
        return;
      }
      getDealerArea('', dealerCodeVal, '').then(res => {
        console.log('查询功能', res);
        if (res.resultCode == 200 && res.data != []) {
          res.data.map(x => {
            if (x.dealerCode == dealerCodeVal) {
              this.formList2.find(item => item.fieldName === 'orgId').initialValue = JSON.stringify(x);
              this.formList2.find(item => item.fieldName === 'ownerName').initialValue = x.dealerId;
            }
          });
          this.formList2.find(item => item.fieldName === 'orgId').itemList = res.data.map(x => ({ value: JSON.stringify(x), text: x.orgName, $code: x.orgCode }));
          let orgId = this.formList2.find(item => item.fieldName == 'orgId').initialValue;
          if (orgId) {
            this.formList2.find(item => item.fieldName === 'ownerName').itemList = res.data.map(x => ({ value: x.dealerId, text: x.dealerShortname, $code: x.dealerCode }));
          }
        }
      });
    },
    closeHandler(val, type) {
      this.visibleDL = val;
      if (type === 'refresh') {
        // 重新加载
        this.fetchParams = { ...this.fetchParams };
      }
    },
    formChangeHandle(val) {
      this.saveData = { ...this.saveData, ...val };
    }
  }
};
</script>

<style lang="less" scoped></style>
